package com.example.chessbattleplatform.web.StorageIo.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chessbattleplatform.Pojo.Exception.BusinessException;
import com.example.chessbattleplatform.Pojo.Exception.StatusCode;
import com.example.chessbattleplatform.web.StorageIo.CopyUtils.CopyFileUtils;
import com.example.chessbattleplatform.web.StorageIo.Pojo.Mapper.FileStorage;
import com.example.chessbattleplatform.web.StorageIo.Pojo.Mapper.UserFileStorage;
import com.example.chessbattleplatform.web.StorageIo.Pojo.Vo.DeleteFileVO;
import com.example.chessbattleplatform.web.StorageIo.Pojo.Vo.UploadFileVo;
import com.example.chessbattleplatform.web.StorageIo.Pojo.Vo.UserFileVo;
import com.example.chessbattleplatform.web.StorageIo.mapper.UserFileMapper;
import com.example.chessbattleplatform.web.StorageIo.service.impl.FileServiceInterface;
import com.example.chessbattleplatform.web.StorageIo.service.impl.UserFileServiceInterface;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor()
@Slf4j
public class UserFileService extends ServiceImpl<UserFileMapper, UserFileStorage> implements UserFileServiceInterface {
    private final FileServiceInterface fileService;
    private final UserFileMapper userFileMapper;
    private final CopyFileUtils copyFileUtils;

    @Override
    @Transactional
    public UserFileVo uploadFile(MultipartFile file, Long id) {
        /*
          添加用户图片 和 修改用户图片 共用一个接口
          有两种可能
          1.用户没用图片
          就直接添加图片
          2.用户有图片
          先删除用户图片在添加图片
         */
        deleteFile(id);

        //添加文件表
        UploadFileVo uploadFileVo = fileService.uploadFile(file);

        //在添加用户与文件关系表
        UserFileStorage userFileStorage = UserFileStorage.builder()
                .userId(id).fileId(uploadFileVo.getFileID())
                .build();

        if (userFileMapper.insert(userFileStorage) <= 0) {
            throw new BusinessException(StatusCode.MISSING_PARAMETER, "文件添加失败");
        }

        //返回数据
        return UserFileVo.builder().uploadFileVo(uploadFileVo).uerId(id).build();
    }

    @Override
    public UserFileVo selectByUuid(Long userId) {

        //查询是否有数据
        LambdaQueryWrapper<UserFileStorage> last = new LambdaQueryWrapper<UserFileStorage>().eq(UserFileStorage::getUserId, userId);
        UserFileStorage userFileStorage = userFileMapper.selectOne(last);

        //处理 用户有图片
        if (userFileStorage != null) {

            FileStorage fileStorage = fileService.selectByFileId(userFileStorage.getFileId());

            if (fileStorage == null) {
                throw new BusinessException(StatusCode.BAD_REQUEST, "文件不存在");
            }

            return UserFileVo.builder()
                    .uploadFileVo(copyFileUtils.FileStorageToUploadFileVo(fileStorage))
                    .uerId(userId)
                    .build();
        }

        return UserFileVo.builder()
                .uerId(userId)
                .build();
    }

    @Override
    @Transactional
    public DeleteFileVO deleteFile(Long userId) {
        //查询是否有数据
        LambdaQueryWrapper<UserFileStorage> last = new LambdaQueryWrapper<UserFileStorage>().eq(UserFileStorage::getUserId, userId);
        UserFileStorage userFileStorage = userFileMapper.selectOne(last);
        log.info("查询结果{}",userFileStorage);
        //处理 用户有图片
        if (userFileStorage != null) {
            //删除文件表
            if (!fileService.deleteFile(userFileStorage.getFileId())) {
                throw new BusinessException(StatusCode.BAD_REQUEST, "文件不存在");
            }
            //在删除文件与用户关系表
            if (userFileMapper.delete(last) <= 0) {
                throw new BusinessException(StatusCode.BAD_REQUEST, "文件不存在");
            }
            return DeleteFileVO.builder()
                    .fileName(userFileStorage.getFileId())
                    .userId(userId).DeleteTime(LocalDateTime.now())
                    .build();
        }

        return DeleteFileVO.builder()
                .userId(userId).DeleteTime(LocalDateTime.now())
                .build();
    }
}
